//@version=5
indicator("Derivative Oscillator w/ cross", shorttitle="Deriv_Osc_cross", overlay=false)

// Input settings
useFixedValue = input.bool(false, title="Fixed Value")
assetType = input.string("forex", title="Asset Type", options=["forex", "crypto", "stocks", "metals"])
timeFrame = input.string("1d", title="Time Frame", options=["4h", "1d"])

// Default input settings
userRSILength = input.int(14, title="RSI Length")
userSMALength = input.int(9, title="SMA Length")
userEMA1Length = input.int(5, title="EMA1 Length")
userEMA2Length = input.int(3, title="EMA2 Length")
priceType = input.string("Close", title="Price Type", options=["Close", "Open", "High", "Low", "Median"])

// Initialize variables
var int RSILength = na
var int SMALength = na
var int EMA1Length = na
var int EMA2Length = na
var string Price = na

// Determine fixed values for each setting based on asset type and time frame
if useFixedValue
    if assetType == "forex" and timeFrame == "1d"
        RSILength := 4
        EMA1Length := 6
        EMA2Length := 2
        SMALength := 82
        Price := "Open"
    else if assetType == "forex" and timeFrame == "4h"
        RSILength := 3
        EMA1Length := 4
        EMA2Length := 2
        SMALength := 2
        Price := "Close"
    else if assetType == "crypto" and timeFrame == "1d"
        RSILength := 5
        EMA1Length := 2
        EMA2Length := 90
        SMALength := 2
        Price := "Open"
    else if assetType == "crypto" and timeFrame == "4h"
        RSILength := 2
        EMA1Length := 2
        EMA2Length := 10
        SMALength := 2
        Price := "Median"
    else if assetType == "metals" and timeFrame == "1d"
        RSILength := 5
        EMA1Length := 2
        EMA2Length := 83
        SMALength := 3
        Price := "High"
    else if assetType == "metals" and timeFrame == "4h"
        RSILength := 2
        EMA1Length := 2
        EMA2Length := 66
        SMALength := 19
        Price := "Median"
    else if assetType == "stocks" and timeFrame == "1d"
        RSILength := 4
        EMA1Length := 12
        EMA2Length := 51
        SMALength := 22
        Price := "Low"
    else if assetType == "stocks" and timeFrame == "4h"
        RSILength := 2
        EMA1Length := 8
        EMA2Length := 5
        SMALength := 8
        Price := "Open"
else
    RSILength := userRSILength
    SMALength := userSMALength
    EMA1Length := userEMA1Length
    EMA2Length := userEMA2Length
    Price := priceType

// Select the appropriate source based on the chosen Price
src = switch Price
    "Close" => close
    "Open" => open
    "High" => high
    "Low" => low
    "Median" => hl2
    => close // Default to close if none match

// Derivative Oscillator Calculation
rsiValue = ta.rsi(src, RSILength)
ema1 = ta.ema(rsiValue, EMA1Length)
ema2 = ta.ema(ema1, EMA2Length)
doValue = ema2 - ta.sma(ema2, SMALength)

// Determine color based on position relative to zero
doColor = doValue > 0 ? color.green : color.red

// Plotting the oscillator with color change
plot(doValue, color=doColor, linewidth=2, title="Derivative Oscillator")
hline(0, "Zero Line", color=color.blue, linewidth=1)

// Crossover/Crossunder for Derivative Oscillator
plotshape(ta.crossover(doValue, 0), title="DO Crossover", location=location.bottom, style=shape.triangleup, size=size.tiny, color=color.green)
plotshape(ta.crossunder(doValue, 0), title="DO Crossunder", location=location.bottom, style=shape.triangledown, size=size.tiny, color=color.red)
